package com.dhcc.bpm.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.entity.DataScope;
import com.dhcc.bpm.modules.system.service.IDataScopeService;
import com.dhcc.bpm.modules.system.vo.DataScopeVO;
import com.dhcc.bpm.modules.system.wrapper.DataScopeWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;

/**
 * @ClassName DataScopeController
 * @Description 数据权限控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/data-scope")
@Api(value = "数据权限", tags = "数据权限")
public class DataScopeController extends BladeController {

	private IDataScopeService dataScopeService;

	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 10:00 2020/2/22 0022
	 * @param dataScope
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.entity.DataScope>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dataScope")
    @ApiLog("数据权限详情")
	@Log(title = "数据权限详情", businessType = BusinessType.SELECT)
	public R<DataScope> detail(DataScope dataScope) {
		DataScope detail = dataScopeService.getOne(Condition.getQueryWrapper(dataScope));
		return R.data(detail);
	}

	/**
	 * @Description 分页
	 * @Author sjm
	 * @Date 10:01 2020/2/22 0022
	 * @param dataScope
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.DataScopeVO>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入dataScope")
    @ApiLog("数据权限分页查询")
	@Log(title = "数据权限分页查询", businessType = BusinessType.SELECT)
	public R<IPage<DataScopeVO>> list(@ApiIgnore @RequestParam Map<String, Object> dataScope, Query query) {
		IPage<DataScope> pages = dataScopeService.page(Condition.getPage(query), Condition.getQueryWrapper(dataScope,DataScope.class));
		return R.data(DataScopeWrapper.build().pageVO(pages));
	}

	/**
	 * @Description 新增
	 * @Author sjm
	 * @Date 10:01 2020/2/22 0022
	 * @param dataScope
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 3)
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@ApiOperation(value = "新增", notes = "传入dataScope")
    @ApiLog("新增数据权限")
	@Log(title = "数据权限", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody DataScope dataScope) {
		return R.status(dataScopeService.save(dataScope));
	}

	/**
	 * @Description 修改
	 * @Author sjm
	 * @Date 10:01 2020/2/22 0022
	 * @param dataScope
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@ApiOperation(value = "修改", notes = "传入dataScope")
    @ApiLog("修改数据权限")
	@Log(title = "数据权限", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody DataScope dataScope) {
		return R.status(dataScopeService.updateById(dataScope));
	}

	/**
	 * @Description 新增或修改
	 * @Author sjm
	 * @Date 10:01 2020/2/22 0022
	 * @param dataScope
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 5)
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@ApiOperation(value = "新增或修改", notes = "传入dataScope")
    @ApiLog("新增或修改数据权限")
	@Log(title = "数据权限", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody DataScope dataScope) {
		return R.status(dataScopeService.saveOrUpdate(dataScope));
	}


	/**
	 * @Description 删除
	 * @Author sjm
	 * @Date 10:01 2020/2/22 0022
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 6)
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
    @ApiLog("删除数据权限")
	@Log(title = "数据权限", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(dataScopeService.deleteLogic(Func.toLongList(ids)));
	}

}
